<?php

namespace app\admin\validate;

use app\common\lib\Captcha;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Validate;

class Project extends Validate
{
    protected $rule = [
        'year' => 'require',
        'category' => 'require|checkCategory',
        'name' => 'require',
        'application_unit'=>'require',
        'principal'=>'require',
        'reference'=>'require',
        'amount'=>'require',
        'area'=>'require|checkCategory',
        'status'=>'require',
        'id'=>'require'
    ];

    protected $scene=[
        'create'=>['year','category','name','application_unit','principal','reference','amount','area','status'],
        'update'=>['id','year','category','name','application_unit','principal','reference','amount','area','status'],
    ];

    protected $message = [
        'year' => '立项年度不能为空',
        'category' => '类别不能为空',
        'name' => '名称不能为空',
        'application_unit' => '申报单位不能为空',
        'principal' => '负责人不能为空',
        'reference' => '推荐单位不能为空',
        'amount' => '金额不能为空',
        'area' => '区域不能为空',
        'status' => '状态不能为空',
        'id' => 'ID不能为空',
    ];

    /**
     * @param $value
     * @param $rule
     * @param array $data
     * @return string|true
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    protected function checkCategory($value, $rule, array $data = [])
    {
        $category=(new \app\common\business\Category())->categoryDictionaries();
        if(!isset($category[$value])){
            return '类别不存在';
        }
        return true;
    }
}
